import 'package:flutter/material.dart';
import 'package:aweb5/base/BaseWidget.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:aweb5/util/WindowUtil.dart';
import 'package:aweb5/bean/AttItem.dart';
import 'package:aweb5/bean/Attr.dart';
//import 'package:flutter_section_table_view/flutter_section_table_view.dart';
import 'package:aweb5/bean/Entry.dart';
import 'package:aweb5/pages/EntryItem.dart';
import 'package:aweb5/pages/ShopCartDialog.dart';
class ProductDetail extends BaseWidget {
  @override
  State<ProductDetail> createState() => ProductDetailState();
}

class ProductDetailState extends BaseWidgetState<ProductDetail> {
  List<String> image;
  List<Attr> att;
  @override
  void initState() {
    super.initState();
    image = List();
    image.add("assets/pic.png");

    List<AttItem> attList = List();
    attList.add(AttItem("芒果"));
    attList.add(AttItem("草莓"));
    attList.add(AttItem("菠萝"));
    Attr attr = Attr("口味", attList);
    att = List();
    att.add(attr);

    List<AttItem> attList1 = List();
    attList.add(AttItem("新加坡"));
    attList.add(AttItem("泰国"));
    attList.add(AttItem("越南"));
    Attr attr1 = Attr("产地", attList1);
    att.add(attr1);
  }

  @override
  Widget build(BuildContext context) {
    init(context);
    return Scaffold(
      appBar: appBarWithBack("商品详情"),
      body: ListView(
        children: <Widget>[
          banner(),
          discript(),
          guige(),
          price(),
          divider(),
          discript(),
          divider(),
          discItem(),
          divider(),
          saleItem(),
          divider(),
          attrItem(),
          bigDivider(),
          imageText(),
          imageTextCon()
        ],
      ),
      bottomSheet: bottom(context),
    );
  }

  Widget banner() {
    return Container(
      width: windowWidth,
      height: windowWidth,
      child: new Swiper(
        itemBuilder: (BuildContext context, int index) {
          return new Image.asset(image[index],
              fit: BoxFit.fill, height: windowWidth);
        },
        itemCount: image.length,
        autoplay: true,
        onIndexChanged: (int s) {
//               setState(() {});
        },
        pagination:
////          new SwiperPagination(builder: SwiperPagination.fraction),
            new SwiperPagination(
                builder: DotSwiperPaginationBuilder(
          color: Colors.black54,
          activeColor: Colors.white,
        )),
      ),
    );
  }

  Widget discript() {
    return Container(
      margin: EdgeInsets.all(9),
      child: Text(
        "每日更新花花牛双口味益生菌风味发酵乳花花牛双口味益生菌风味发酵乳早餐酸奶180g",
        style: TextStyle(color: Color(0xff222222), fontSize: 13),
      ),
    );
  }

  Widget guige() {
    return Container(
      margin: EdgeInsets.only(left: 9),
      child: Text(
        "规格",
        style: TextStyle(color: Color(0xff888888), fontSize: 11),
      ),
    );
  }

  Widget price() {
    return Container(
      margin: EdgeInsets.only(left: 9, top: 9, bottom: 9),
      child: Text(
        "¥9",
        style: TextStyle(
            color: Color(0xffff0000),
            fontSize: 18,
            fontWeight: FontWeight.bold),
      ),
    );
  }

  Widget divider() {
    return Container(
      height: 1,
      color: Color(0xffe9e9e9),
    );
  }

  Widget discItem() {
    return Container(
      margin: EdgeInsets.only(left: 9),
      alignment: Alignment.centerLeft,
      height: 39,
      child: Row(
        children: <Widget>[
          Text(
            "描述",
            style: TextStyle(color: Color(0xff888888), fontSize: 12),
          ),
          SizedBox(
            width: 20,
          ),
          Text(
            "描述内容",
            style: TextStyle(color: Color(0xff222222), fontSize: 12),
          ),
        ],
      ),
    );
  }

  Widget saleItem() {
    return Container(
      margin: EdgeInsets.only(left: 9),
      alignment: Alignment.centerLeft,
      height: 39,
      child: Row(
        children: <Widget>[
          Text(
            "服务",
            style: TextStyle(color: Color(0xff888888), fontSize: 12),
          ),
          SizedBox(
            width: 20,
          ),
          Text(
            "正品保证 • 假一赔十 • 7天包退换",
            style: TextStyle(color: Color(0xff222222), fontSize: 12),
          ),
        ],
      ),
    );
  }

  Widget attrItem() {
    return GestureDetector(
      child: Container(
        margin: EdgeInsets.only(left: 9),
        alignment: Alignment.centerLeft,
        height: 39,
        child: Row(
          children: <Widget>[
            Text(
              "属性",
              style: TextStyle(color: Color(0xff888888), fontSize: 12),
            ),
            SizedBox(
              width: 20,
            ),
            Text(
              "选择属性",
              style: TextStyle(color: Color(0xff222222), fontSize: 12),
            ),
            Expanded(child: Text("")),
            Image.asset(
              "assets/rightarrow.png",
              width: 10,
              height: 19,
            )
          ],
        ),
      ),
      onTap: () {},
    );
  }

  Widget bigDivider() {
    return Container(
      height: 5,
      color: Color(0xfff4f4f4),
    );
  }

  Widget imageText() {
    return Container(
      alignment: Alignment.center,
      height: 36,
      child: Text(
        "图文详情",
        style: TextStyle(
            color: Color(0xff000000),
            fontSize: 11,
            fontWeight: FontWeight.bold),
      ),
    );
  }

  Widget imageTextCon() {
    return Container(
      height: 326,
      child: FadeInImage.assetNetwork(
        width: windowWidth,
        height: 326,
        placeholder: "assets/imagetext.png",
        image: "",
        fit: BoxFit.fill,
      ),
    );
  }

  Widget bottom(context) {
    return Container(
      height: 49,
      child: Row(
        children: <Widget>[
          Expanded(
              child: GestureDetector(
            child: Container(
              alignment: Alignment.center,
              height: 49,
              color: Color(0xffffbf27),
              child: Text(
                "加入购物车",
                style: TextStyle(color: Colors.white, fontSize: 16),
              ),
            ),
            onTap: () {},
          )),
          Expanded(
              child: GestureDetector(
            child: Container(
              alignment: Alignment.center,
              height: 49,
              color: Color(0xffff8900),
              child: Text(
                "立即购买",
                style: TextStyle(color: Colors.white, fontSize: 16),
              ),
            ),
            onTap: () {
              showBottomSheet(context);
            },
          ))
        ],
      ),
    );
  }

  void showBottomSheet(context) {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return ShopCartDialog(data);
        });
  }
  
  //购物车弹出
  Widget goToBuyContent() {
    return Container(
      padding: EdgeInsets.only(top: 15),
      height: 445,
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              SizedBox(
                width: 10,
              ),
              FadeInImage.assetNetwork(
                  width: 100,
                  height: 100,
                  fit: BoxFit.fill,
                  placeholder: "assets/productimg.png",
                  image: ""),
              SizedBox(
                width: 10,
              ),
              Expanded(
                child: Container(
                    height: 100,
                    width: 100,
                    child: Stack(
                      children: <Widget>[
                        Positioned(
                            right: 0,
                            top: 0,
                            child: InkWell(
                              child: Container(
                                width: 32,
                                height: 32,
                                padding: EdgeInsets.all(10),
                                child: Image.asset(
                                  "assets/close.png",
                                  width: 12,
                                  height: 12,
                                ),
                              ),
                              onTap: () {
                                navigatorState.pop();
                              },
                            )),
                        Positioned(
                          child: Text(
                            "¥8",
                            style: TextStyle(
                                color: Color(0xffff0000), fontSize: 18),
                          ),
                          left: 0,
                          top: 12,
                        ),
                        Positioned(
                          child: Container(
                            width: windowWidth - 100 - 30,
                            child: Text(
                              "每日更新花花牛双口味益生菌风味发酵乳花花牛双口味益生菌风餐酸奶180g ",
                              style: TextStyle(
                                  color: Color(0xff2d2d2d), fontSize: 13),
                              maxLines: 2,
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                          left: 0,
                          top: 50,
                        )
                      ],
                    )),
              )
            ],
          ),
          SizedBox(
            height: 10,
          ),
          Divider(
            height: 3,
            color: Color(0xffe5e5e5),
            indent: 10,
            endIndent: 10,
          ),
          new Expanded(
              child: ListView.builder(
            padding: EdgeInsets.only(bottom: 50),
            shrinkWrap: true,
//            physics: NeverScrollableScrollPhysics(),
            itemBuilder: (BuildContext context, int index) {
              return expandList(index);
            },
            itemCount: data.length,
          )),
        ],
      ),
    );
  }

  Widget expandList(index) {
    return CustomScrollView(
      physics: NeverScrollableScrollPhysics(),
      shrinkWrap: true,
      slivers: <Widget>[
        SliverToBoxAdapter(
          child: Text(
            data[index].title,
          ),
        ),

        SliverGrid(
          delegate: SliverChildBuilderDelegate((
            context,
            index1,
          ) {
            return buildItem(index1, index);
          },childCount: data[index].children.length),
         gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
        ),
//        SliverList(
//          delegate: SliverChildBuilderDelegate((
//            context,
//            index1,
//          ) {
//            return buildItem(index1, index);
//          }, childCount: data[index].children.length),
//        ),
      ],
    );
  }

  Widget buildItem(index1, index) {
    return Text(data[index].children[index1].title);
  }

// The entire multilevel list displayed by this app.
  final List<Entry> data = <Entry>[
    new Entry(
      '口味',
      false,
      <Entry>[

        new Entry('桃子',false),
        new Entry('西瓜',false),
        new Entry('栗子',false),
        new Entry('水果',false),
        new Entry('西瓜',false),
      ],
    ),
    new Entry(
      '产地',
        false,
      <Entry>[
        new Entry('新西兰',false),
        new Entry('加拿大',false),
      ],
    ),

  ];
}
